package com.edu.ujn.myh.wf;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.TaskService;
import org.jbpm.api.activity.ActivityExecution;
import org.jbpm.api.activity.ExternalActivityBehaviour;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
import org.jbpm.pvm.internal.history.HistoryEvent;
import org.jbpm.pvm.internal.history.events.TaskActivityStart;
import org.jbpm.pvm.internal.model.Activity;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.Transition;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.edu.ujn.myh.action.BaseAction;
import com.edu.ujn.myh.service.IApplyService;

/**
 * 
 * @作者 马永华
 * @日期 2010-4-19下午04:20:49
 * @版本 1.0.0
 * @说明 多人会签
 */
public class Custom extends BaseAction implements ExternalActivityBehaviour {

	private IApplyService applyService = null;

	// 动态添加专家会签
	public void execute(ActivityExecution activityExecution) throws Exception {
		ExecutionImpl executionimpl = (ExecutionImpl) activityExecution;

		DbSession dbsession = EnvironmentImpl.getFromCurrent(DbSession.class);
		TaskImpl taskimpl = dbsession.createTask();
		taskimpl.setName("ZJHQ");
		taskimpl.setExecution(executionimpl);
		taskimpl.setProcessInstance(executionimpl.getProcessInstance());
		taskimpl.setSignalling(false);
		taskimpl.setExecutionDbid(executionimpl.getDbid());
		dbsession.save(taskimpl);
		HistoryEvent.fire(new TaskActivityStart(taskimpl), executionimpl);
		// 从session中获得专家ID数组
		Map map = (Map) getRequest().getSession().getAttribute("profe");
		Set keySet = map.keySet();
		for (Iterator iter = keySet.iterator(); iter.hasNext();) {
			String id = (String) iter.next();
			TaskImpl subtaskimpl1 = taskimpl.createSubTask();
			subtaskimpl1.setName("专家会签");
			subtaskimpl1.setAssignee(id);
			subtaskimpl1.setSignalling(false);
			subtaskimpl1.setVariable("articleList", map.get(id));
			subtaskimpl1.setExecutionDbid(executionimpl.getDbid());
			dbsession.save(subtaskimpl1);
			HistoryEvent.fire(new TaskActivityStart(subtaskimpl1),
					executionimpl);
		}
		executionimpl.waitForSignal();
	}

	public void signal(ActivityExecution activityExecution, String signalName,
			Map<String, ?> parms) throws Exception {
		ExecutionImpl executionImpl = (ExecutionImpl) activityExecution;
		ProcessInstance pi = (ProcessInstance) executionImpl
				.getProcessInstance();
		TaskService taskService = Configuration.getProcessEngine()
				.getTaskService();
		Activity activity = executionImpl.getActivity();
		List<Task> tasks = taskService.createTaskQuery().processInstanceId(
				pi.getId()).list();

		List<Task> subTask_list = taskService.getSubTasks(tasks.get(0).getId());
		Iterator<Task> iter = subTask_list.iterator();
		while (iter.hasNext()) {
			Task subtask = iter.next();
			if (parms.get("userId").equals(subtask.getAssignee())) {
				taskService.completeTask(subtask.getId());
			}
		}
		if (subTask_list == null || subTask_list.size() == 1) {
			Transition transition = null;
			if ((signalName == null)
					|| ((Task.STATE_COMPLETED.equals(signalName))
							&& (activity.getOutgoingTransitions() != null) && (activity
							.getOutgoingTransitions().size() == 1))) {
				transition = activity.getOutgoingTransitions().get(0);
			} else {
				transition = activity.findOutgoingTransition(signalName);
			}
			taskService.completeTask(tasks.get(0).getId());
			executionImpl.take(transition);
		} else {
			executionImpl.waitForSignal();
		}
	}

	public void setApplyService(IApplyService applyService) {
		this.applyService = applyService;
	}
}
